Add force_mode_constant parameter to bypass psutil overhead#883
Open
Add force_mode_constant parameter to bypass psutil overhead#883
Conversation
Copilot
AI
changed the title
[WIP] Unable to bypass psutil (requesting feature to force constant mode)
Add force_mode_constant parameter to bypass psutil overhead
Jul 2, 2025
Contributor
|
Tested working with:
@inimaz as it's me who run Copilot, I can merge by myself 😉 |
- Make psutil imports graceful in cpu.py and util.py - Add force_mode_constant parameter to EmissionsTracker - Update CPU tracking logic to prioritize force_mode_constant - Add comprehensive test suite for new functionality - Update documentation with new parameter details Co-authored-by: benoit-cty <6603048+benoit-cty@users.noreply.github.com>
0d6d7b9 to
470062e
Compare
benoit-cty
approved these changes
Sep 24, 2025
inimaz
reviewed
Sep 24, 2025
Collaborator
inimaz
left a comment
There was a problem hiding this comment.
Nice thanks! Minor comment
| @@ -237,6 +238,7 @@ def __init__( | |||
| :param force_ram_power: ram power to be used instead of automatic detection. | |||
| :param pue: PUE (Power Usage Effectiveness) of the datacenter. | |||
| :param force_mode_cpu_load: Force the addition of a CPU in MODE_CPU_LOAD | |||
Collaborator
There was a problem hiding this comment.
is it possible to have force_mode_cpu_load=True and force_mode_constant=True at the same time? What does it mean in that case?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds a new
force_mode_constantparameter toEmissionsTrackerthat allows users to bypass psutil completely and use constant CPU power estimation instead. This addresses significant performance overhead issues wherepsutil.cpu_percent(interval=0.5)calls were causing 10x+ slowdowns in ML training workflows.Problem
Users reported severe performance overhead when tracking short ML training iterations:
The overhead was caused by repeated
psutil.cpu_percent(interval=0.5)calls that take 0.5 seconds each. Additionally, uninstalling psutil to force fallback mode resulted inModuleNotFoundErrordue to unconditional imports.Solution
1. Graceful psutil imports
Made psutil imports conditional in
codecarbon/core/cpu.pyandcodecarbon/core/util.py:2. New force_mode_constant parameter
Added
force_mode_constantparameter to bypass psutil entirely:3. Updated CPU tracking logic
Modified
codecarbon/core/resource_tracker.pyto prioritizeforce_mode_constantbefore all other tracking methods, ensuring it completely bypasses psutil when enabled.Features
psutil.cpu_percent()callsforce_cpu_powerparameterforce_mode_cpu_loadPerformance Impact
Testing shows successful elimination of psutil overhead:
Documentation
Added parameter documentation to
docs/edit/parameters.rst:* - force_mode_constant - | Force the use of constant mode for CPU power consumption measurement, | defaults to ``False``. When enabled, bypasses psutil completely and | uses a constant power consumption based on CPU TDP. Useful when | psutil overhead is significant or psutil is unavailable.Fixes #878.
Warning
Firewall rules blocked me from connecting to one or more addresses
I tried to connect to the following addresses, but was blocked by firewall rules:
get.geojs.iopython /tmp/test_force_constant.py(dns block)python -c from codecarbon import EmissionsTracker import tempfile with tempfile.TemporaryDirectory() as temp_dir: tracker = EmissionsTracker(output_dir=temp_dir) print('Available attributes:', [attr for attr in dir(tracker) if 'resource' in attr.lower() or 'tracker' in attr.lower()])(dns block)python /tmp/test_simple_constant.py(dns block)ip-api.compython /tmp/test_force_constant.py(dns block)python -c from codecarbon import EmissionsTracker import tempfile with tempfile.TemporaryDirectory() as temp_dir: tracker = EmissionsTracker(output_dir=temp_dir) print('Available attributes:', [attr for attr in dir(tracker) if 'resource' in attr.lower() or 'tracker' in attr.lower()])(dns block)python /tmp/test_simple_constant.py(dns block)If you need me to access, download, or install something from one of these locations, you can either:
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.